using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public delegate void cboCurSelected(string aselec);

public partial class Controles_cboCursos : System.Web.UI.UserControl
{
    public event cboCurSelected seleciona;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ddlCursos.Items.Count == 0)
            rendercboCursos();

        if (IsPostBack)
            rendercboMaterias();
    }

    public string value
    {
        get { 
                if (ddlMaterias.Items.Count != 0)
                    return ddlMaterias.SelectedItem.Value;
                else
                    return "";
        
            }
    }

    private void rendercboMaterias()
    {
        SqlConnection cnn;
        cnn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ColegiosConnectionString"].ToString());
        string sCommand = @"select distinct M.IdMateria, M.MateriaDesc from Materia M
                            inner join MateriasCurso MC on (MC.IdCurso = " + ddlCursos.SelectedValue + ")";
        
        sCommand += @"inner join ProfesorMaterias PM on (PM.IdMateria = M.IdMateria and PM.IdProfesorMateria = MC.IdProfesorMateria)
                            order by M.MateriaDesc";

        SqlCommand oCommand = new SqlCommand(sCommand, cnn);
        cnn.Open();
        SqlDataReader dr = oCommand.ExecuteReader();
        int ddlMateriaSelected = ddlMaterias.SelectedIndex;
        ddlMaterias.Items.Clear();
        int icount = 0;
        if (dr.HasRows)
        {
            ddlMaterias.Items.Insert(icount, new ListItem("Seleccione...", "-1"));
            while (dr.Read())
            {
                icount++;
                ddlMaterias.Items.Insert(icount, new ListItem(dr.GetString(1), dr.GetInt32(0).ToString()));
            }
        }

        ddlMaterias.SelectedIndex = ddlMateriaSelected;
    }

    private void rendercboCursos()
    {
        SqlConnection cnn;
        cnn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ColegiosConnectionString"].ToString());
        string sCommand = "select * from Curso where IdSede = " + biFactory.User().IdSede.ToString() + " order by IdCurso";
        SqlCommand oCommand = new SqlCommand(sCommand, cnn);
        cnn.Open();
        SqlDataReader dr = oCommand.ExecuteReader();

        int icount = 0;
        if (dr.HasRows)
        {
            ddlCursos.Items.Insert(icount, new ListItem("Seleccione...", "-1"));
            while (dr.Read())
            {
                icount++;
                ddlCursos.Items.Insert(icount, new ListItem(dr.GetString(1), dr.GetInt32(0).ToString()));
            }
        }
        if (seleciona != null)
        { // El evento se lanza como cualquier delegado
            seleciona.Invoke("select");
        }
    }

    protected void ddlMaterias_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (seleciona != null)
        { // El evento se lanza como cualquier delegado
            seleciona.Invoke("select");
        }
    }
    protected void ddlCursos_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (seleciona != null)
        { // El evento se lanza como cualquier delegado
            seleciona.Invoke("select");
        }
    }
}
